//
//  MainViewController.m
//  SinaBlog
//
//  Created by mac on 15/11/24.
//  Copyright © 2015年 ZZY. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "DiscoverTableViewController.h"
#import "ProfileTableViewController.h"


@interface MainViewController ()

// tabBar 加号按钮
@property (nonatomic, strong) UIButton *composeButton;


@end

@implementation MainViewController

#pragma mark - 懒加载控件
// 1. 懒加载 tabBar 加号按钮
-(UIButton *)composeButton {
    if (_composeButton== nil) {
        _composeButton = [[UIButton alloc]init];
        
        // 添加自定义tabBarItem到tabBar
        [self.tabBar addSubview:self.composeButton];
        CGFloat counnt = self.tabBar.subviews.count;
        CGFloat w = self.tabBar.bounds.size.width / (counnt - 1);
        _composeButton.frame = CGRectInset(self.tabBar.bounds, 2 * w - 2, 0);
        
        [ _composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"]forState:UIControlStateNormal];
        
        [ _composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"]forState:UIControlStateHighlighted];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:(UIControlStateNormal)];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:(UIControlStateHighlighted)];
        
      [_composeButton addTarget:self action:@selector(clickComposeButton) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _composeButton;
    
}

#pragma mark - 监听方法
// 坚听加号按钮的点击
- (void)clickComposeButton {
    NSLog(@"点击进入发布微博");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 添加所有子控制器
    [self addChildVc];

}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tabBar bringSubviewToFront:self.composeButton];

}

#pragma mark - 创建子控制器

// MARK : 添加所有子控制器
     
- (void)addChildVc {
    
    [self addChildVcWithVc:[[HomeTableViewController alloc]init] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_highlighted"];
    
    [self addChildVcWithVc:[[MessageTableViewController alloc]init] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_highlighted"] ;
    
    [self addChildViewController:[[UIViewController alloc]init]];
    
    [self addChildVcWithVc:[[DiscoverTableViewController alloc]init] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_highlighted"];
    
    [self addChildVcWithVc:[[ProfileTableViewController alloc]init]title:@"我的" image:@"tabbar_profile" selectedImage:@"tabbar_profile_highlighted" ];
    
    }

- (void)addChildVcWithVc:(UIViewController *)Vc title:(NSString *)title image: (NSString *)image selectedImage:(NSString *)selectedImage {
    
    // 0.
    self.tabBar.tintColor = [UIColor orangeColor];
    
    // 1. 设置title
    Vc.title = title;
    
    // 2. 包装子控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Vc];
    
    // 3. 设置tabbar
    Vc.tabBarItem.image = [UIImage imageNamed:image];
    Vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    // 4. 设置tabBarController的子控制器
    [self addChildViewController:nav];
}

@end
